﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.CaiWu;
using Enterprise.Data.CaiWu;
namespace Enterprise.Service.Caiwubu
{
    public class CwZbService
    {

        /// <summary>
        ///  缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-CwZb";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<CwZbEntity> CwZbList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<CwZbEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                CwZbData data = new CwZbData();
                List<CwZbEntity> list = data.CwZbList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回记录信息
        /// </summary>
        /// <param name="zbId">指标编码</param>
        /// <returns></returns>
        public CwZbEntity CwZbDisp(int zbId)
        {
            return CwZbList().Where(p => p.ZBID == zbId).FirstOrDefault();
        }



        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }


        /// <summary>
        /// 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void CwZbExecute(CwZbEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into CWB_ZB(ZBID,ZBTYPEID,ZBCODE,ZBNAME,ZBNAMERU,ZBUNIT,ZBQZ,ZBORDERBY) values(SEQ_CWB_TYPE.nextval," + entity.ZBTYPEID + ",'" + entity.ZBCODE + "','" + entity.ZBNAME + "','" + entity.ZBNAMERU + "','" + entity.ZBUNIT + "'," + entity.ZBQZ + "," + entity.ZBORDERBY + ")";
                    break;
                case "Update":
                    cmd = "Update CWB_ZB set ZBCODE='" + entity.ZBCODE + "',ZBNAME='" + entity.ZBNAME + "',ZBNAMERU='" + entity.ZBNAMERU + "',ZBUNIT='" + entity.ZBUNIT + "',ZBQZ=" + entity.ZBQZ + " where ZBID=" + entity.ZBID;
                    break;
                case "Delete":
                    cmd = "Delete from CWB_ZB where ZBID=" + entity.ZBID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                CwZbExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 组织机构操作
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1更新 0不更新</param>
        public void CwZbExecuteBySql(string cmd, int typeId)
        {
            CwTypeData data = new CwTypeData();
            data.CwTypeExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        #region "返回指标名称"
        /// <summary>
        /// 返回名称
        /// </summary>
        /// <param name="deptId">编号</param>
        /// <returns></returns>
        public static string GetZbName(int zbId)
        {
            CwZbService dService = new CwZbService();
            var q = dService.CwZbDisp(zbId);
            if (q != null)
            {
                return q.ZBNAME;
            }
            return "";
        }
        #endregion

    }
}
